package org.achicu.db.rhino;

import org.achicu.db.values.StorableObject;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.WrapFactory;

public class StorableWrapFactory extends WrapFactory {

	public StorableWrapFactory() {
		super();
		setJavaPrimitiveWrap(false);
	}

	public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
			Object javaObject, Class staticType) {
		if(javaObject instanceof StorableObject){
			return new RhinoStorableObject((StorableObject)javaObject, scope);
		}else{
			return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
		}
	}
	
	public static void initStandardWrappers(Context context, ScriptableObject scope){
		if(! (context.getWrapFactory() instanceof StorableWrapFactory) ) {
			context.setWrapFactory(new StorableWrapFactory());
		}
		
		ScriptableObject.putProperty(scope, "StorableObject", new StorableObjectConstructor());
		ScriptableObject.putProperty(scope, "StorableCollection", new StorableCollectionConstructor());
	}

}
